package org.jeecg.modules.charge.service.impl;

import org.jeecg.modules.charge.entity.ChargeInvoiceMangroup;
import org.jeecg.modules.charge.entity.ChargeInvoiceMangroupType;
import org.jeecg.modules.charge.entity.ChargeInvoiceMangroupPosition;
import org.jeecg.modules.charge.entity.ChargeInvoiceManpeople;
import org.jeecg.modules.charge.mapper.ChargeInvoiceMangroupTypeMapper;
import org.jeecg.modules.charge.mapper.ChargeInvoiceMangroupPositionMapper;
import org.jeecg.modules.charge.mapper.ChargeInvoiceManpeopleMapper;
import org.jeecg.modules.charge.mapper.ChargeInvoiceMangroupMapper;
import org.jeecg.modules.charge.service.IChargeInvoiceMangroupService;
import org.jeecg.modules.charge.vo.CheckOutMangroup;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 票据组
 * @Author: jeecg-boot
 * @Date:   2019-07-08
 * @Version: V1.0
 */
@Service
public class ChargeInvoiceMangroupServiceImpl extends ServiceImpl<ChargeInvoiceMangroupMapper, ChargeInvoiceMangroup> implements IChargeInvoiceMangroupService {

	@Autowired
	private ChargeInvoiceMangroupMapper chargeInvoiceMangroupMapper;
	@Autowired
	private ChargeInvoiceMangroupTypeMapper chargeInvoiceMangroupTypeMapper;
	@Autowired
	private ChargeInvoiceMangroupPositionMapper chargeInvoiceMangroupPositionMapper;
	@Autowired
	private ChargeInvoiceManpeopleMapper chargeInvoiceManpeopleMapper;
	
	@Override
	@Transactional
	public void saveMain(ChargeInvoiceMangroup chargeInvoiceMangroup, List<ChargeInvoiceMangroupType> chargeInvoiceMangroupTypeList,List<ChargeInvoiceMangroupPosition> chargeInvoiceMangroupPositionList,List<ChargeInvoiceManpeople> chargeInvoiceManpeopleList) {
		chargeInvoiceMangroupMapper.insert(chargeInvoiceMangroup);
		for(ChargeInvoiceMangroupType entity:chargeInvoiceMangroupTypeList) {
			//外键设置
			entity.setGroupId(chargeInvoiceMangroup.getId());
			chargeInvoiceMangroupTypeMapper.insert(entity);
		}
		for(ChargeInvoiceMangroupPosition entity:chargeInvoiceMangroupPositionList) {
			//外键设置
			entity.setGroupId(chargeInvoiceMangroup.getId());
			chargeInvoiceMangroupPositionMapper.insert(entity);
		}
		for(ChargeInvoiceManpeople entity:chargeInvoiceManpeopleList) {
			//外键设置
			entity.setGroupId(chargeInvoiceMangroup.getId());
			chargeInvoiceManpeopleMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(ChargeInvoiceMangroup chargeInvoiceMangroup,List<ChargeInvoiceMangroupType> chargeInvoiceMangroupTypeList,List<ChargeInvoiceMangroupPosition> chargeInvoiceMangroupPositionList,List<ChargeInvoiceManpeople> chargeInvoiceManpeopleList) {
		chargeInvoiceMangroupMapper.updateById(chargeInvoiceMangroup);
		
		//1.先删除子表数据
		chargeInvoiceMangroupTypeMapper.deleteByMainId(chargeInvoiceMangroup.getId());
		chargeInvoiceMangroupPositionMapper.deleteByMainId(chargeInvoiceMangroup.getId());
		chargeInvoiceManpeopleMapper.deleteByMainId(chargeInvoiceMangroup.getId());
		
		//2.子表数据重新插入
		for(ChargeInvoiceMangroupType entity:chargeInvoiceMangroupTypeList) {
			//外键设置
			entity.setGroupId(chargeInvoiceMangroup.getId());
			chargeInvoiceMangroupTypeMapper.insert(entity);
		}
		for(ChargeInvoiceMangroupPosition entity:chargeInvoiceMangroupPositionList) {
			//外键设置
			entity.setGroupId(chargeInvoiceMangroup.getId());
			chargeInvoiceMangroupPositionMapper.insert(entity);
		}
		for(ChargeInvoiceManpeople entity:chargeInvoiceManpeopleList) {
			//外键设置
			entity.setGroupId(chargeInvoiceMangroup.getId());
			chargeInvoiceManpeopleMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		chargeInvoiceMangroupTypeMapper.deleteByMainId(id);
		chargeInvoiceMangroupPositionMapper.deleteByMainId(id);
		chargeInvoiceManpeopleMapper.deleteByMainId(id);
		chargeInvoiceMangroupMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			chargeInvoiceMangroupTypeMapper.deleteByMainId(id.toString());
			chargeInvoiceMangroupPositionMapper.deleteByMainId(id.toString());
			chargeInvoiceManpeopleMapper.deleteByMainId(id.toString());
			chargeInvoiceMangroupMapper.deleteById(id);
		}
	}

	@Override
	public List<ChargeInvoiceMangroup> selectByLimit(String userId) {
		return chargeInvoiceMangroupMapper.selectByLimit(userId);
	}

	@Override
	public Long duplicateCheckCountSql(CheckOutMangroup checkOutMangroup) {
		return chargeInvoiceMangroupMapper.duplicateCheckCountSql(checkOutMangroup);
	}

	@Override
	public Long duplicateCheckCountSqlNoDataId(CheckOutMangroup checkOutMangroup) {
		return chargeInvoiceMangroupMapper.duplicateCheckCountSqlNoDataId(checkOutMangroup);
	}

	@Override
	public Long duplicateCheckPositionCountSql(CheckOutMangroup checkOutMangroup) {
		return chargeInvoiceMangroupMapper.duplicateCheckPositionCountSql(checkOutMangroup);
	}

	@Override
	public Long duplicateCheckPositionCountSqlNoDataId(CheckOutMangroup checkOutMangroup,String[] offId) {
		return chargeInvoiceMangroupMapper.duplicateCheckPositionCountSqlNoDataId(checkOutMangroup,offId);
	}

	@Override
	public Long duplicateCheckPeopleCountSql(CheckOutMangroup checkOutMangroup) {
		return chargeInvoiceMangroupMapper.duplicateCheckPeopleCountSql(checkOutMangroup);
	}

	@Override
	public Long duplicateCheckPeopleCountSqlNoDataId(CheckOutMangroup checkOutMangroup, String[] userId) {
		return chargeInvoiceMangroupMapper.duplicateCheckPeopleCountSqlNoDataId(checkOutMangroup,userId);
	}

}
